package com.woniu.controller;

import com.nimbusds.jose.JOSEException;
import com.woniu.entity.User;
import com.woniu.service.UserService;
import com.woniu.util.ResponseData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {
    private UserService userService;
    /**
     * 1: 接受请求
     * 2: 校验参数
     * 3: 调service方法
     * 4: 响应
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResponseData<?> login(@RequestBody User user){
        try {
            ResponseData<?> login = userService.login(user);
            return login;
        } catch (JOSEException e) {
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/addCart/{id}/{buyNum}")
    public ResponseData<?> addCart(@PathVariable("id") Integer id ,@PathVariable("buyNum")Integer buyNum){
        try {
            return userService.addCart(id,buyNum);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    @GetMapping("/queryCartAndAddr")
    public ResponseData<?> queryCartAndAddr(){
        try {
            return userService.queryCartAndAddr();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
